using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using YunxSoft.Abp.LanguageManagement.EntityFrameworkCore;
using YunxSoft.Abp.LanguageManagement.MultiTenancy;
using YunxSoft.Abp.LanguageManagement.Web;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.Account;
using Volo.Abp.Account.Web;
// using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.Autofac;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Emailing;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.SqlServer;
using Volo.Abp.FeatureManagement;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Identity.Web;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.PermissionManagement;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.HttpApi;
using Volo.Abp.PermissionManagement.Identity;
using Volo.Abp.SettingManagement;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.Swashbuckle;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.Web;
using Volo.Abp.VirtualFileSystem;

namespace YunxSoft.Abp.LanguageManagement;

[DependsOn(
    typeof(LanguageManagementWebModule),
    typeof(LanguageManagementApplicationModule),
    typeof(LanguageManagementHttpApiModule),
    typeof(LanguageManagementEntityFrameworkCoreModule),
    typeof(AbpAuditLoggingEntityFrameworkCoreModule),
    typeof(AbpAutofacModule),
    typeof(AbpAccountWebModule),
    typeof(AbpAccountApplicationModule),
    typeof(AbpAccountHttpApiModule),
    typeof(AbpEntityFrameworkCoreSqlServerModule),
    typeof(AbpSettingManagementEntityFrameworkCoreModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpPermissionManagementApplicationModule),
    typeof(AbpPermissionManagementHttpApiModule),
    typeof(AbpIdentityWebModule),
    typeof(AbpIdentityApplicationModule),
    typeof(AbpIdentityHttpApiModule),
    typeof(AbpIdentityEntityFrameworkCoreModule),
    typeof(AbpPermissionManagementDomainIdentityModule),
    typeof(AbpFeatureManagementWebModule),
    typeof(AbpFeatureManagementApplicationModule),
    typeof(AbpFeatureManagementHttpApiModule),
    typeof(AbpFeatureManagementEntityFrameworkCoreModule),
    typeof(AbpTenantManagementWebModule),
    typeof(AbpTenantManagementApplicationModule),
    typeof(AbpTenantManagementHttpApiModule),
    typeof(AbpTenantManagementEntityFrameworkCoreModule),
    // typeof(AbpAspNetCoreMvcUiBasicThemeModule),
    typeof(AbpAspNetCoreMvcUiLeptonXLiteThemeModule),
    typeof(AbpAspNetCoreSerilogModule),
    typeof(AbpSwashbuckleModule)
    )]
public class LanguageManagementWebUnifiedModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var hostingEnvironment = context.Services.GetHostingEnvironment();
        var configuration = context.Services.GetConfiguration();

        Configure<AbpDbContextOptions>(options =>
        {
            options.UseSqlServer();
        });

        if (hostingEnvironment.IsDevelopment())
        {
            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                options.FileSets.ReplaceEmbeddedByPhysical<LanguageManagementDomainSharedModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}YunxSoft.Abp.LanguageManagement.Domain.Shared", Path.DirectorySeparatorChar)));
                options.FileSets.ReplaceEmbeddedByPhysical<LanguageManagementDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}YunxSoft.Abp.LanguageManagement.Domain", Path.DirectorySeparatorChar)));
                options.FileSets.ReplaceEmbeddedByPhysical<LanguageManagementApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}YunxSoft.Abp.LanguageManagement.Application.Contracts", Path.DirectorySeparatorChar)));
                options.FileSets.ReplaceEmbeddedByPhysical<LanguageManagementApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}YunxSoft.Abp.LanguageManagement.Application", Path.DirectorySeparatorChar)));
                options.FileSets.ReplaceEmbeddedByPhysical<LanguageManagementWebModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}YunxSoft.Abp.LanguageManagement.Web", Path.DirectorySeparatorChar)));
            });
        }

        context.Services.AddAbpSwaggerGen(
            options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "LanguageManagement API", Version = "v1" });
                options.DocInclusionPredicate((docName, description) => true);
                options.CustomSchemaIds(type => type.FullName);
            });
        
        context.Services.Replace(ServiceDescriptor.Transient<ILanguageProvider, DatabaseLanguageProvider>());

        Configure<AbpMultiTenancyOptions>(options =>
        {
            options.IsEnabled = MultiTenancyConsts.IsEnabled;
        });

#if DEBUG
        context.Services.Replace(ServiceDescriptor.Singleton<IEmailSender, NullEmailSender>());
#endif
    }

    public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseErrorPage();
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();

        if (MultiTenancyConsts.IsEnabled)
        {
            app.UseMultiTenancy();
        }

        app.UseAbpRequestLocalization();
        app.UseAuthorization();

        app.UseSwagger();
        app.UseAbpSwaggerUI(options =>
        {
            options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support APP API");
        });

        app.UseAuditing();
        app.UseAbpSerilogEnrichers();
        app.UseConfiguredEndpoints();

        using (var scope = context.ServiceProvider.CreateScope())
        {
            await scope.ServiceProvider
                .GetRequiredService<IDataSeeder>()
                .SeedAsync();
        }
    }

    // public class MyDataSeeder : IDataSeeder, ITransientDependency
    // {
    //     private readonly ILanguageRepository _languageRepository;
    //     private readonly IGuidGenerator _guidGenerator;
    //     private readonly ICurrentTenant _currentTenant;
    //     private readonly ISettingManager _settingManager;
    //
    //     public MyDataSeeder(ILanguageRepository languageRepository, IGuidGenerator guidGenerator, ICurrentTenant currentTenant, ISettingManager settingManager)
    //     {
    //         _languageRepository = languageRepository;
    //         _guidGenerator = guidGenerator;
    //         _currentTenant = currentTenant;
    //         _settingManager = settingManager;
    //     }
    //     
    //     public async Task SeedAsync(DataSeedContext context)
    //     {
    //         var list = await _languageRepository.GetListAsync(cancellationToken: CancellationToken.None);
    //         if (list.Count == 0)
    //         {
    //             // await _languageRepository.DeleteManyAsync(list);
    //
    //             var addList = new List<Language>
    //             {
    //                 new(_guidGenerator.Create(), "ar", "ar", "العربية", "", true),
    //                 new(_guidGenerator.Create(), "cs", "cs", "Čeština", "", true),
    //                 new(_guidGenerator.Create(), "en", "en", "English", "", true),
    //                 new(_guidGenerator.Create(), "en-GB", "en-GB", "English (UK)", "", true),
    //                 new(_guidGenerator.Create(), "fi", "fi", "Finnish", "", true),
    //                 new(_guidGenerator.Create(), "fr", "fr", "Français", "", true),
    //                 new(_guidGenerator.Create(), "hi", "hi", "Hindi", "", true),
    //                 new(_guidGenerator.Create(), "is", "is", "Icelandic", "", true),
    //                 new(_guidGenerator.Create(), "it", "it", "Italiano", "", true),
    //                 new(_guidGenerator.Create(), "hu", "hu", "Magyar", "", true),
    //                 new(_guidGenerator.Create(), "pt-BR", "pt-BR", "Português (Brasil)", "", true),
    //                 new(_guidGenerator.Create(), "ro-RO", "ro-RO", "Română", "", true),
    //                 new(_guidGenerator.Create(), "ru", "ru", "Русский", "", true),
    //                 new(_guidGenerator.Create(), "sk", "sk", "Slovak", "", true),
    //                 new(_guidGenerator.Create(), "tr", "tr", "Türkçe", "", true),
    //                 new(_guidGenerator.Create(), "zh-Hans", "zh-Hans", "简体中文", "", true),
    //                 new(_guidGenerator.Create(), "zh-Hant", "zh-Hant", "繁體中文", "", true),
    //                 new(_guidGenerator.Create(), "de-DE", "de-DE", "Deutsch", "", true),
    //                 new(_guidGenerator.Create(), "es", "es", "Español", "", true),
    //                 new(_guidGenerator.Create(), "el", "el", "Ελληνικά", "", true),
    //             };
    //             
    //             await _languageRepository.InsertManyAsync(addList);
    //         
    //         }
    //
    //     }
    // }
}
